<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>欢迎来到guo的博客!</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta property="og:type" content="website">
<meta property="og:title" content="欢迎来到guo的博客!">
<meta property="og:url" content="http://shangguan-lingfeng.gitee.io/blog1/page/3/index.html">
<meta property="og:site_name" content="欢迎来到guo的博客!">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="Jerry">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/blog1/atom.xml" title="欢迎来到guo的博客!" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/blog1/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/blog1/css/style.css">

  
    
<link rel="stylesheet" href="/blog1/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/blog1/" id="logo">欢迎来到guo的博客!</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/blog1/">Home</a>
        
          <a class="main-nav-link" href="/blog1/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/blog1/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Suche"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Suche"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://shangguan-lingfeng.gitee.io/blog1"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main">
  
    <article id="post-JavaScript操作JSON常用方法" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/04/09/JavaScript%E6%93%8D%E4%BD%9CJSON%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/" class="article-date">
  <time class="dt-published" datetime="2021-04-09T08:06:49.590Z" itemprop="datePublished">2021-04-09</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/04/09/JavaScript%E6%93%8D%E4%BD%9CJSON%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/">JavaScript操作JSON常用方法</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h3 id="一、概念简介"><a href="#一、概念简介" class="headerlink" title="一、概念简介"></a>一、概念简介</h3><p>JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式，采用完全独立于语言的文本格式，是理想的数据交换格式。同时，JSON是 JavaScript 原生格式，这意味着在 JavaScript 中处理 JSON数据不须要任何特殊的 API 或工具包。<br>在JSON中，有两种结构：对象和数组。</p>
<h4 id="1-对象"><a href="#1-对象" class="headerlink" title="1.对象"></a>1.对象</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> packJson = &#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Liza&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;123&quot;</span>&#125;;</span><br></pre></td></tr></table></figure>
<p>一个对象以“{”开始，“}”结束，“key/value”之间运用 “,”分隔。</p>
<h4 id="2-数组"><a href="#2-数组" class="headerlink" title="2.数组"></a>2.数组</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> packJson = [&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Liza&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;123&quot;</span>&#125;, &#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Mike&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;456&quot;</span>&#125;];</span><br></pre></td></tr></table></figure>
<p>数组是值的有序集合。一个数组以“[”开始，“]”结束。值之间运用 “,”分隔。</p>
<h3 id="二、JSON对象和JSON字符串的转换"><a href="#二、JSON对象和JSON字符串的转换" class="headerlink" title="二、JSON对象和JSON字符串的转换"></a>二、JSON对象和JSON字符串的转换</h3><p>在数据传输过程中，JSON是以字符串的形式传递的，而JS操作的是JSON对象，所以，JSON对象和JSON字符串之间的相互转换是关键。例如：</p>
<p>JSON字符串：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonStr =<span class="string">&#x27;&#123;&quot;name&quot;:&quot;Liza&quot;, &quot;password&quot;:&quot;123&quot;&#125;&#x27;</span> ;</span><br></pre></td></tr></table></figure>
<p>JSON对象：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonObj = &#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Liza&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;123&quot;</span>&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="1、-JSON字符串转化-JSON对象"><a href="#1、-JSON字符串转化-JSON对象" class="headerlink" title="1、 JSON字符串转化 JSON对象"></a>1、 JSON字符串转化 JSON对象</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonObject= jQuery.parseJSON(jsonstr);</span><br></pre></td></tr></table></figure>
<h4 id="2、-JSON对象转化-JSON-字符串"><a href="#2、-JSON对象转化-JSON-字符串" class="headerlink" title="2、 JSON对象转化 JSON 字符串"></a>2、 JSON对象转化 JSON 字符串</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonstr =<span class="built_in">JSON</span>.stringify(jsonObject );</span><br></pre></td></tr></table></figure>
<h3 id="三、JSON的输出美化"><a href="#三、JSON的输出美化" class="headerlink" title="三、JSON的输出美化"></a>三、JSON的输出美化</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">JSON.stringify(value[, replacer [, space]])</span><br></pre></td></tr></table></figure>
<p>上一节讲到了将JSON转化为字符串可以用JSON.stringify() 方法，stringify还有个可选参数space，可以指定缩进用的空白字符串，用于美化输出（pretty-print）；<br>space参数是个数字，它代表有多少的空格；上限为10。该值若小于1，则意味着没有空格；如果该参数没有提供（或者为null）将没有空格。</p>
<p>我们看下代码：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> formatJsonStr=<span class="built_in">JSON</span>.stringify(jsonObject,<span class="literal">undefined</span>, <span class="number">2</span>);</span><br></pre></td></tr></table></figure>

<p>待美化的JSON：<br><img src="https://img-blog.csdn.net/20161028115259937" alt="美化前的JSON"></p>
<p>美化后的效果：<br><img src="https://img-blog.csdn.net/20161028115303650" alt="美化后的JSON"></p>
<h3 id="四、JSON字符串的替换"><a href="#四、JSON字符串的替换" class="headerlink" title="四、JSON字符串的替换"></a>四、JSON字符串的替换</h3><p>工作经常遇到这样的字符串，如下：<br><img src="https://img-blog.csdn.net/20161028115308822" alt="替换前的JSON"></p>
<p>需要经过替换后，才能从字符串转化成JSON对象。这里我们需要用JS实现replaceAll的功能， 将所有的 ‘ \“ ‘ 替换成 ‘ “ ‘ .<br>代码如下,这里的gm是固定的，g表示global，m表示multiple：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonStr=jsonStr.replace(<span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&#x27;\\&quot;&#x27;</span>,<span class="string">&quot;gm&quot;</span>), <span class="string">&#x27;&quot;&#x27;</span> );</span><br></pre></td></tr></table></figure>
<p>替换后的效果如下：<br><img src="https://img-blog.csdn.net/20161028115312432" alt="替换后的JSON"></p>
<h3 id="五、遍历JSON对象和JSON数组"><a href="#五、遍历JSON对象和JSON数组" class="headerlink" title="五、遍历JSON对象和JSON数组"></a>五、遍历JSON对象和JSON数组</h3><h4 id="1、遍历JSON对象代码如下："><a href="#1、遍历JSON对象代码如下：" class="headerlink" title="1、遍历JSON对象代码如下："></a>1、遍历JSON对象代码如下：</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> packJson  = &#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Liza&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;123&quot;</span>&#125; ;</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> k <span class="keyword">in</span> packJson )&#123;<span class="comment">//遍历packJson 对象的每个key/value对,k为key</span></span><br><span class="line">   alert(k + <span class="string">&quot; &quot;</span> + packJson[k]);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="2、遍历JSON数组代码如下："><a href="#2、遍历JSON数组代码如下：" class="headerlink" title="2、遍历JSON数组代码如下："></a>2、遍历JSON数组代码如下：</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> packJson = [&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Liza&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;123&quot;</span>&#125;, &#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Mike&quot;</span>, <span class="string">&quot;password&quot;</span>:<span class="string">&quot;456&quot;</span>&#125;];</span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i <span class="keyword">in</span> packJson)&#123;<span class="comment">//遍历packJson 数组时，i为索引</span></span><br><span class="line">   alert(packJson[i].name + <span class="string">&quot; &quot;</span> + packJson[i].password);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="六、递归遍历"><a href="#六、递归遍历" class="headerlink" title="六、递归遍历"></a>六、递归遍历</h3><p>为了实现一些复杂功能常常需要递归遍历JSON对象，这里给出两个递归的例子，希望能给大家参考递归的写法。</p>
<h4 id="1、第一个例子是递归遍历JSON，遇到数组的时候，数组中有超过一个对象，删除第一个对象之后的所有对象"><a href="#1、第一个例子是递归遍历JSON，遇到数组的时候，数组中有超过一个对象，删除第一个对象之后的所有对象" class="headerlink" title="1、第一个例子是递归遍历JSON，遇到数组的时候，数组中有超过一个对象，删除第一个对象之后的所有对象"></a>1、第一个例子是递归遍历JSON，遇到数组的时候，数组中有超过一个对象，删除第一个对象之后的所有对象</h4><p>举个例子，原始JSON如下：<br><img src="https://img-blog.csdn.net/20161028115320046" alt="原始JSON"></p>
<p>期望处理后的JSON如下：<br><img src="https://img-blog.csdn.net/20161028115324375" alt="处理后的JSON"></p>
<p>递归代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> *返回处理后的 json字符串</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">jsonParse</span>(<span class="params">jsonObj</span>) </span>&#123;</span><br><span class="line">     </span><br><span class="line">      distinctJson(jsonObj);</span><br><span class="line">      <span class="keyword">var</span> last=<span class="built_in">JSON</span>.stringify(jsonObj, <span class="literal">undefined</span>, <span class="number">2</span>);</span><br><span class="line">      <span class="keyword">return</span> last;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

 <figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 去掉 json中数组多余的项</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">distinctJson</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(obj <span class="keyword">instanceof</span> <span class="built_in">Array</span>) &#123;</span><br><span class="line">             <span class="keyword">if</span>(obj.length &gt; <span class="number">1</span>) &#123; <span class="comment">//数组中有超过一个对象，删除第一个对象之后的所有对象</span></span><br><span class="line">                  obj.splice(<span class="number">1</span>, (obj.length - <span class="number">1</span>));</span><br><span class="line">            &#125;</span><br><span class="line">            distinctJson(obj[<span class="number">0</span>]);</span><br><span class="line">      &#125; <span class="keyword">else</span> <span class="keyword">if</span>(obj <span class="keyword">instanceof</span> <span class="built_in">Object</span>) &#123;</span><br><span class="line">             <span class="keyword">for</span>( <span class="keyword">var</span> index <span class="keyword">in</span> obj)&#123;</span><br><span class="line">                   <span class="keyword">var</span> jsonValue = obj[index];</span><br><span class="line">                  distinctJson(jsonValue);</span><br><span class="line">            &#125;</span><br><span class="line">      &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2、第二个例子是递归查找目标节点（节点id为targetId，有且只有一个），找到后把targetChildren赋值给节点的children，"><a href="#2、第二个例子是递归查找目标节点（节点id为targetId，有且只有一个），找到后把targetChildren赋值给节点的children，" class="headerlink" title="2、第二个例子是递归查找目标节点（节点id为targetId，有且只有一个），找到后把targetChildren赋值给节点的children，"></a>2、第二个例子是递归查找目标节点（节点id为targetId，有且只有一个），找到后把targetChildren赋值给节点的children，</h4><p>举个例子，原始JSON如下，查找的目标节点id为5：<br><img src="https://img-blog.csdn.net/20161028115329119" alt="原始JSON"></p>
<p>targetChildren为<br><img src="https://img-blog.csdn.net/20161028115334578" alt="targetChildren的JSON"></p>
<p>期望最后处理的JSON结果为：<br><img src="https://img-blog.csdn.net/20161028115338995" alt="期望后的JSON"></p>
<p>递归代码如下：</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 递归查找目标节点</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">findTarget</span>(<span class="params">obj,targetId,targetChildren</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(obj.id==targetId)&#123;</span><br><span class="line">          obj.children=targetChildren;</span><br><span class="line">          <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">          <span class="keyword">if</span>(obj.children!=<span class="literal">null</span>)&#123;</span><br><span class="line">               <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="number">0</span>; i&lt;obj.children.length; i++)&#123;</span><br><span class="line">                    <span class="keyword">var</span> flag=findTarget(obj.children[i],targetId,targetChildren);</span><br><span class="line">                    <span class="keyword">if</span>(flag==<span class="literal">true</span>)&#123;</span><br><span class="line">                          <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">               &#125;</span><br><span class="line">          &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>————————————————<br>版权声明：本文为CSDN博主「徐小妖」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。<br>原文链接：<a target="_blank" rel="noopener" href="https://blog.csdn.net/xujie3/article/details/52954940">https://blog.csdn.net/xujie3/article/details/52954940</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/04/09/JavaScript%E6%93%8D%E4%BD%9CJSON%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95/" data-id="cknublgbc0003v8uhhbs56scy" data-title="JavaScript操作JSON常用方法" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-gitee提交到他人仓库" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/04/07/gitee%E6%8F%90%E4%BA%A4%E5%88%B0%E4%BB%96%E4%BA%BA%E4%BB%93%E5%BA%93/" class="article-date">
  <time class="dt-published" datetime="2021-04-07T13:37:10.703Z" itemprop="datePublished">2021-04-07</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/04/07/gitee%E6%8F%90%E4%BA%A4%E5%88%B0%E4%BB%96%E4%BA%BA%E4%BB%93%E5%BA%93/">gitee提交到他人仓库</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>前言</p>
<p>一直使用 idea 克隆和上传提交项目，今天需要把项目上传到另外一个仓库中，发现 Git bash 操作都忘了如何上传项目了，所以准备在这里记录一下，不太常用，一般是换电脑的时候用用。</p>
<p>关键字：Git、gitee、gitbash</p>
<p>一、新建一个本地文件夹仓库</p>
<p>名字随意，说白了就是一个新建文件夹(如果你的项目没有被 Git 管理过，可以就用当前项目文件夹作为仓库，我这里被管理过了，想上传到另外的仓库所以就新建了)</p>
<p>二、在当前文件夹点击右键，进入 gitbash(没有的需要百度下载)</p>
<p>三、执行初始化语句，让这个普通文件夹不再普通(说人话就是，让这个文件夹被 git 所管理)</p>
<p>$ git init</p>
<p>Initialized empty Git repository in D:/workplace/cnjy-parent/.git/</p>
<p>成功后会出现一个 .git 的隐藏文件夹</p>
<p>四、把你需要上传的文件夹拷贝到这个新建的文件夹中</p>
<p>由于我是把项目重新上传到另外一个仓库，所以我把我以前文件夹中的除了 .git 文件夹除外的所有东西拷贝到新文件夹了</p>
<p>以自己的为准</p>
<p>五、添加到缓存区</p>
<p>$ git add . // .代表所有文件都添加</p>
<p>六、提交到本地仓库</p>
<p>$ git commit -m “chuniao” // 引号里面是备注信息，随便写</p>
<p>七、关联远程仓库</p>
<p>1、gitee 新建仓库</p>
<p>然后创建一个和文件夹同名的仓库</p>
<p>2、复制地址</p>
<p>3、执行关联命令</p>
<p>$ git remote add origin <a target="_blank" rel="noopener" href="https://gitee.com/RyanChaw/cnjy-parent.git">https://gitee.com/RyanChaw/cnjy-parent.git</a> # <a target="_blank" rel="noopener" href="https://gitee.com/RyanChaw/cnjy-parent.git">https://gitee.com/RyanChaw/cnjy-parent.git</a> 是刚刚复制的</p>
<p>八、如果 gitee 仓库创建的额时候有 README.MD 文件，则先拉取代码，如果仓库是空的则跳过</p>
<p>$ git pull –rebase origin master</p>
<p>九、提交到远程仓库</p>
<p>git push -u origin master</p>
<p>十、查看远程仓库</p>
<p>此时就能看到远程仓库里有刚刚提交到的代码了</p>
<p>总结</p>
<p>本来 idea 也可以上传代码到远程仓库的，github、gitee 差不多，但是如果已经控制的版本，经常会遇到一些预料不到的错误情况，这个时候就需要用到 gitbash 了，亦可以都用 gitbash，比 idea 的功能齐全嘛，所以也有必要掌握一下。<br>————————————————<br>版权声明：本文为CSDN博主「weixin_39805364」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。<br>原文链接：<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_39805364/article/details/111854806">https://blog.csdn.net/weixin_39805364/article/details/111854806</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/04/07/gitee%E6%8F%90%E4%BA%A4%E5%88%B0%E4%BB%96%E4%BA%BA%E4%BB%93%E5%BA%93/" data-id="cknublgbl000hv8uh0qmnb4yb" data-title="gitee提交到他人仓库" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/gitee/" rel="tag">gitee</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-sshd三方客户端登录" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/04/02/sshd%E4%B8%89%E6%96%B9%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%99%BB%E5%BD%95/" class="article-date">
  <time class="dt-published" datetime="2021-04-02T03:50:17.338Z" itemprop="datePublished">2021-04-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/04/02/sshd%E4%B8%89%E6%96%B9%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%99%BB%E5%BD%95/">sshd三方客户端登录</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="1-查看服务器IP命令"><a href="#1-查看服务器IP命令" class="headerlink" title="1.查看服务器IP命令"></a>1.查看服务器IP命令</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ip addr</span><br></pre></td></tr></table></figure>
<p>查找 ens33网卡的IP<br>2. 安装sshd服务</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">查看状态：</span><br><span class="line">	sudo systemctl   status  sshd</span><br><span class="line">	unit sshd.service could not be found    说明该服务器没有安装sshd服务</span><br><span class="line">	如果查看是  Active: active (running)   表示服务可用</span><br><span class="line">安装过程:</span><br><span class="line">	#  更新源</span><br><span class="line">	sudo apt update   </span><br><span class="line">	</span><br><span class="line">	# 安装服务</span><br><span class="line">	sudo apt install openssh-server</span><br></pre></td></tr></table></figure>
<h2 id="3、putty与XShell"><a href="#3、putty与XShell" class="headerlink" title="3、putty与XShell"></a>3、putty与XShell</h2><p>安装putty，输入IP、port 可以登录。</p>
<p>XShell同理</p>
<h2 id="4、常用命令"><a href="#4、常用命令" class="headerlink" title="4、常用命令"></a>4、常用命令</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">路径操作</span><br><span class="line">pwd  查看当前路径</span><br><span class="line">ls  目录下的内容</span><br><span class="line">cd  进入目录</span><br><span class="line">	.  当前目录</span><br><span class="line">	.. 上级目录</span><br><span class="line">	~  用户主目录</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">文件、目录</span><br><span class="line">vi 文件   创建或打开文件</span><br><span class="line">	要编辑文件  按i键 可以编辑文件</span><br><span class="line">	退出编辑文件，按Esc键  </span><br><span class="line">	然后输入:</span><br><span class="line">		冒号：后输入 q   表示退出(非强制)</span><br><span class="line">		        输入 w   表示保存</span><br><span class="line">			输入wq   保存并退出</span><br><span class="line">			输入!q   强制退出(如果有更改也不保存，直接退出</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/04/02/sshd%E4%B8%89%E6%96%B9%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%99%BB%E5%BD%95/" data-id="cknublgbx000xv8uh5x7hdajz" data-title="sshd三方客户端登录" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/CentOS/" rel="tag">CentOS</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-linux常用命令" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/03/30/linux%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" class="article-date">
  <time class="dt-published" datetime="2021-03-30T10:38:59.550Z" itemprop="datePublished">2021-03-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/03/30/linux%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">linux常用命令</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="1-linux常用命令"><a href="#1-linux常用命令" class="headerlink" title="1.linux常用命令"></a>1.linux常用命令</h1><h3 id="1-1-系统命令"><a href="#1-1-系统命令" class="headerlink" title="1.1 系统命令"></a>1.1 系统命令</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">runlevel                     <span class="comment"># 查看当前的运行级别（5代表图形化界面）</span></span><br><span class="line">systemctl status sshd        <span class="comment"># 开启网络服务功能</span></span><br><span class="line">          stop               <span class="comment"># 关闭</span></span><br><span class="line">          restart            <span class="comment"># 重启</span></span><br><span class="line">          reload             <span class="comment"># 重载</span></span><br><span class="line"><span class="comment"># 常见服务</span></span><br><span class="line">sshd                         <span class="comment"># ssh服务（远程xshell连接服务器的）</span></span><br><span class="line">networking                   <span class="comment"># 网络</span></span><br><span class="line"></span><br><span class="line">reboot                       <span class="comment"># 重启</span></span><br><span class="line">halt                         <span class="comment"># 关机</span></span><br><span class="line">poweroff                     <span class="comment"># 关机</span></span><br></pre></td></tr></table></figure>
<h3 id="1-2-查看文件常用指令"><a href="#1-2-查看文件常用指令" class="headerlink" title="1.2 查看文件常用指令"></a>1.2 查看文件常用指令</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">touch file.txt        <span class="comment"># 创建文件</span></span><br><span class="line">vim file.txt          <span class="comment"># 写入文件</span></span><br><span class="line">cat                   <span class="comment"># 在命令提示符下查看文件内容</span></span><br><span class="line">more                  <span class="comment"># 在命令提示符中分页查看文件内容</span></span><br><span class="line">less                  <span class="comment"># 命令行中查看文件可以上下翻页反复浏览</span></span><br><span class="line">head                  <span class="comment"># 命令行中查看文件头几行</span></span><br><span class="line">tail                  <span class="comment"># 命令行中查看文件尾几行</span></span><br><span class="line">wc                    <span class="comment"># 统计文件的单词数 行数等信息</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-目录管理常用指令"><a href="#1-3-目录管理常用指令" class="headerlink" title="1.3 目录管理常用指令"></a>1.3 目录管理常用指令</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&#x27;&#x27;&#x27;1.文件目录常用命令&#x27;&#x27;&#x27;</span></span><br><span class="line">mkdir                 <span class="comment"># 创建新目录</span></span><br><span class="line">rmdir                 <span class="comment"># 删除空目录</span></span><br><span class="line">pwd                   <span class="comment"># 查看你当前所在的目录</span></span><br><span class="line">cd                    <span class="comment"># 切换目录</span></span><br><span class="line">ls                    <span class="comment"># 查看显示目录的内容</span></span><br><span class="line">rm                    <span class="comment"># 删除文件</span></span><br><span class="line">cp                    <span class="comment"># 复制文件或目录</span></span><br><span class="line">mv                    <span class="comment"># 移动文件或目录</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">du                    <span class="comment"># 统计目录和文件空间的占用情况</span></span><br><span class="line">ln                    <span class="comment"># 创建硬链接</span></span><br><span class="line">ln -s                 <span class="comment"># 创建软链接</span></span><br><span class="line">which                 <span class="comment"># 查看linux命令所在的目录</span></span><br></pre></td></tr></table></figure>
<h3 id="1-4-账号与权限"><a href="#1-4-账号与权限" class="headerlink" title="1.4 账号与权限"></a>1.4 账号与权限</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&#x27;&#x27;&#x27;1.组管理&#x27;&#x27;&#x27;</span></span><br><span class="line">groupadd group_name                            <span class="comment"># 创建一个新用户组 </span></span><br><span class="line">groupdel group_name                            <span class="comment"># 删除一个用户组 </span></span><br><span class="line">groupmod -n new_group_name old_group_name      <span class="comment"># 重命名一个用户组</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;2.用户管理&#x27;&#x27;&#x27;</span></span><br><span class="line">useradd zhangsan                               <span class="comment"># 创建账户张三</span></span><br><span class="line">passwd zhangsan                                <span class="comment"># 给用户设置密码</span></span><br><span class="line">userdel -r zhangsan                            <span class="comment"># 删除张三及他的宿主目录</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;3.用户组管理&#x27;&#x27;&#x27;</span></span><br><span class="line">gpasswd -a zhangsan root                       <span class="comment"># 将张三用户加入root组</span></span><br><span class="line">groups zhangsan                                <span class="comment"># 确认zhangsan用户在root组</span></span><br><span class="line">gpasswd -d lisi root                           <span class="comment"># 将李zhangsan户从root组中删除</span></span><br><span class="line"></span><br><span class="line"><span class="string">&#x27;&#x27;&#x27;4.权限管理&#x27;&#x27;&#x27;</span></span><br><span class="line">chown -R zhangsan /aaa                         <span class="comment"># 将文件夹/aaa的多有者修改为zhangsan</span></span><br><span class="line">chown root:root /aaa                           <span class="comment"># 将/aaa文件夹的属主和属组都改成root</span></span><br><span class="line">chmod <span class="number">777</span> /aaa                 </span><br></pre></td></tr></table></figure>
<h1 id="2-ubuntu开启sshd服务"><a href="#2-ubuntu开启sshd服务" class="headerlink" title="2.ubuntu开启sshd服务"></a>2.ubuntu开启sshd服务</h1><ul>
<li>安装sshd</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">root@dev:shiyanlou_project<span class="comment"># sudo apt-get install openssh-server</span></span><br><span class="line">root@dev:shiyanlou_project<span class="comment"># /etc/init.d/ssh status</span></span><br><span class="line">root@dev:shiyanlou_project<span class="comment"># netstat -anptu | grep sshd</span></span><br></pre></td></tr></table></figure>
<ul>
<li>开启root远程登录功能</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sudo vim /etc/ssh/sshd_config</span><br><span class="line"></span><br><span class="line">Port <span class="number">22</span>               <span class="comment"># 去掉注释</span></span><br><span class="line"><span class="comment">#PermitRootLogin prohibit-password</span></span><br><span class="line">PermitRootLogin yes   <span class="comment"># 把上面的换成yes</span></span><br></pre></td></tr></table></figure>
<ul>
<li>重启sshd服务</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">root@dev:shiyanlou_project<span class="comment">#  service sshd restart</span></span><br></pre></td></tr></table></figure>
<h1 id="3-ubuntu-安装软件"><a href="#3-ubuntu-安装软件" class="headerlink" title="3.ubuntu 安装软件"></a>3.ubuntu 安装软件</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 类似pip安装</span></span><br><span class="line">sudo apt-get install XXX</span><br><span class="line">sudo apt-get install -y XXX</span><br><span class="line">sudo apt-get remove XXX</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/03/30/linux%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" data-id="cknublgbo000nv8uh6esocf8r" data-title="linux常用命令" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/linux/" rel="tag">linux</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-解决vue重复点击相同路由" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/03/26/%E8%A7%A3%E5%86%B3vue%E9%87%8D%E5%A4%8D%E7%82%B9%E5%87%BB%E7%9B%B8%E5%90%8C%E8%B7%AF%E7%94%B1/" class="article-date">
  <time class="dt-published" datetime="2021-03-26T12:12:00.243Z" itemprop="datePublished">2021-03-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/03/26/%E8%A7%A3%E5%86%B3vue%E9%87%8D%E5%A4%8D%E7%82%B9%E5%87%BB%E7%9B%B8%E5%90%8C%E8%B7%AF%E7%94%B1/">解决vue重复点击路由</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h3 id="解决-Vue-重复点击相同路由，出现-Uncaught-in-promise-NavigationDuplicated-Avoided-redundant-navigation-问题"><a href="#解决-Vue-重复点击相同路由，出现-Uncaught-in-promise-NavigationDuplicated-Avoided-redundant-navigation-问题" class="headerlink" title="解决 Vue 重复点击相同路由，出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题"></a>解决 Vue 重复点击相同路由，出现 Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation 问题</h3><p>问题描述：重复点击导航时，控制台出现报错 ，虽然不影响功能使用，但也不能视而不见。</p>
<p>解决方案：在跳转时，判断是否跳转路由和当前路由是否一致，避免重复跳转产生问题。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">toMenu</span>(<span class="params">item</span>)</span>&#123;</span><br><span class="line">  <span class="keyword">if</span>(<span class="built_in">this</span>.$route.path!==item.url)&#123;</span><br><span class="line">    <span class="built_in">this</span>.$router.push(&#123;<span class="attr">path</span>:item.url&#125;)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们可以定义上面的方法来解决问题,下面是我的实现代码</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;width: 800px;height: 30px;background: aqua&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;router_page(&#x27;/stulist&#x27;)&quot;</span>&gt;</span>作者<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">button</span> @<span class="attr">click</span>=<span class="string">&quot;router_page(&#x27;/newslist&#x27;)&quot;</span>&gt;</span>新闻<span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">router-view</span>/&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span></span><br><span class="line"><span class="javascript">  name: <span class="string">&#x27;App&#x27;</span>,</span></span><br><span class="line">  methods:&#123;</span><br><span class="line"><span class="javascript">    <span class="function"><span class="title">router_page</span>(<span class="params">url</span>)</span>&#123;</span></span><br><span class="line"><span class="javascript">      <span class="keyword">if</span> (<span class="built_in">this</span>.$route.path !== url)&#123;</span></span><br><span class="line"><span class="javascript">        <span class="built_in">this</span>.$router.push(url)</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/03/26/%E8%A7%A3%E5%86%B3vue%E9%87%8D%E5%A4%8D%E7%82%B9%E5%87%BB%E7%9B%B8%E5%90%8C%E8%B7%AF%E7%94%B1/" data-id="cknublgca001ov8uh12985f4l" data-title="解决vue重复点击路由" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-python小数据池" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/03/26/python%E5%B0%8F%E6%95%B0%E6%8D%AE%E6%B1%A0/" class="article-date">
  <time class="dt-published" datetime="2021-03-26T07:54:43.200Z" itemprop="datePublished">2021-03-26</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/03/26/python%E5%B0%8F%E6%95%B0%E6%8D%AE%E6%B1%A0/">python小数据池</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p><strong>首先，先说一下 “==” “is” “id”，因为下面说小数据池时会用到</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = <span class="number">300</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = <span class="number">300</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s1 == s2)           <span class="comment"># == 判断两个对象是否相等，大家应该都没有异议</span></span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s1 <span class="keyword">is</span> s2)           <span class="comment"># is 判断两个值的内存地址。</span></span><br><span class="line"><span class="literal">False</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(s1)</span><br><span class="line"><span class="number">4360482576</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(s2)</span><br><span class="line"><span class="number">4360482608</span>                    <span class="comment"># 为什么都是300，指向的内存地址不一样呢？</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = <span class="number">100</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s1 == s2)</span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s1 <span class="keyword">is</span> s2)           </span><br><span class="line"><span class="literal">True</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(s1)</span><br><span class="line"><span class="number">4356850720</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="built_in">id</span>(s2)                   <span class="comment"># 值为100，这一次为什么内存地址又一样了呢</span></span><br><span class="line"><span class="number">4356850720</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">A Python program <span class="keyword">is</span> constructed <span class="keyword">from</span> code blocks. A block <span class="keyword">is</span> a piece of Python program text that <span class="keyword">is</span> </span><br><span class="line">executed <span class="keyword">as</span> a unit. </span><br><span class="line">The following are blocks: a module, a function body, <span class="keyword">and</span> a <span class="class"><span class="keyword">class</span> <span class="title">definition</span>. <span class="title">Each</span> <span class="title">command</span> <span class="title">typed</span></span></span><br><span class="line"><span class="class"><span class="title">interactively</span> <span class="title">is</span> <span class="title">a</span> <span class="title">block</span>.</span></span><br><span class="line"><span class="class"><span class="title">A</span> <span class="title">script</span> <span class="title">file</span> (<span class="params">a file given <span class="keyword">as</span> standard <span class="built_in">input</span> to the interpreter <span class="keyword">or</span> specified <span class="keyword">as</span> a command line argument</span></span></span><br><span class="line"><span class="class"><span class="params">to the interpreter</span>) <span class="title">is</span> <span class="title">a</span> <span class="title">code</span> <span class="title">block</span>. <span class="title">A</span> <span class="title">script</span> <span class="title">command</span></span></span><br><span class="line"><span class="class">(<span class="params">a command specified on the interpreter command line <span class="keyword">with</span> the ‘-c‘ option</span>) <span class="title">is</span> <span class="title">a</span> <span class="title">code</span> <span class="title">block</span>. </span></span><br><span class="line"><span class="class"><span class="title">The</span> <span class="title">string</span> <span class="title">argument</span> <span class="title">passed</span> <span class="title">to</span> <span class="title">the</span> <span class="title">built</span>-<span class="title">in</span> <span class="title">functions</span> <span class="title">eval</span>() <span class="title">and</span> <span class="title">exec</span>() <span class="title">is</span> <span class="title">a</span> <span class="title">code</span> <span class="title">block</span>.</span></span><br><span class="line"><span class="class"><span class="title">A</span> <span class="title">code</span> <span class="title">block</span> <span class="title">is</span> <span class="title">executed</span> <span class="title">in</span> <span class="title">an</span> <span class="title">execution</span> <span class="title">frame</span>. <span class="title">A</span> <span class="title">frame</span> <span class="title">contains</span> <span class="title">some</span> <span class="title">administrative</span> <span class="title">information</span></span></span><br><span class="line"><span class="class">(<span class="params">used <span class="keyword">for</span> debugging</span>) <span class="title">and</span> <span class="title">determines</span> <span class="title">where</span> <span class="title">and</span> <span class="title">how</span> <span class="title">execution</span> <span class="title">continues</span> <span class="title">after</span> <span class="title">the</span> <span class="title">code</span> <span class="title">block</span>’<span class="title">s</span> <span class="title">execution</span></span></span><br><span class="line"><span class="class"><span class="title">has</span> <span class="title">completed</span>.</span></span><br></pre></td></tr></table></figure>
<p>上面的主要意思是：</p>
<p>Python程序是由代码块构造的。块是一个python程序的文本，他是作为一个单元执行的。</p>
<p>代码块：一个模块，一个函数，一个类，一个文件等都是一个代码块。</p>
<p>而作为交互方式输入的每个命令都是一个代码块。</p>
<p>什么叫交互方式？就是咱们在cmd中进入Python解释器里面，每一行代码都是一个代码块，例如：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = <span class="number">300</span>        <span class="comment"># 这是一个代码块</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = <span class="number">300</span>        <span class="comment"># 这也是一个代码块</span></span><br><span class="line"> </span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">func1</span>():</span></span><br><span class="line"><span class="meta">... </span>    <span class="keyword">pass</span></span><br><span class="line"><span class="meta">... </span>                  <span class="comment"># 此处的函数体是一个代码块</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="function"><span class="keyword">def</span> <span class="title">func2</span>():</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&quot;123&quot;</span>)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">&quot;guo&quot;</span>)</span><br><span class="line"><span class="meta">... </span>                  <span class="comment"># 此处的函数体也是一个代码块</span></span><br><span class="line"> </span><br><span class="line">➜  ~ ls|grep *.py</span><br><span class="line">awa.py                <span class="comment"># 此文件是一个代码块</span></span><br></pre></td></tr></table></figure>
<h3 id="代码块缓存机制"><a href="#代码块缓存机制" class="headerlink" title="代码块缓存机制"></a>代码块缓存机制</h3><p>为了节省内存，提高效率，python有小数据池的概念，意思为当你执行一个代码块时，python会先检查这个代码块是否存在，存在则重用，不存在则新建，<br>这也就是为什么上面你生成了s1、s2两个变量，变量值都为100，但是内存地址一样的原因，也就是id相同，但是为什么当等于300的时候则不一样，<br>也就是新建了呢？那是因为小数据池的缓存机制是有使用范围的：</p>
<p>代码块的缓存机制的适用范围：<strong>int（float），str，bool。</strong><br><strong>int(float):任何数字在同一代码块下都会复用。</strong></p>
<p><strong>bool:True和False在字典中会以1，0方式存在，并且复用。</strong></p>
<p><strong>str：几乎所有的字符串都会符合缓存机制</strong></p>
<h3 id="缓存机制"><a href="#缓存机制" class="headerlink" title="缓存机制"></a>缓存机制</h3><p>小数据池，也称为小整数缓存机制，或者称为驻留机制等等.</p>
<p><strong>大前提：小数据池也是只针对 int(float)，str，bool。</strong></p>
<p><strong>小数据池是针对不同代码块之间的缓存机制！！！</strong></p>
<p>官方对于整数，字符串的小数据池是这么说的:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 对于整数，Python官方文档中这么说：</span></span><br><span class="line">The current implementation keeps an array of integer objects <span class="keyword">for</span> <span class="built_in">all</span> integers between -<span class="number">5</span> <span class="keyword">and</span> <span class="number">256</span>, when you</span><br><span class="line">create an <span class="built_in">int</span> <span class="keyword">in</span> that <span class="built_in">range</span> you actually just get back a reference to the existing <span class="built_in">object</span>. So it should be</span><br><span class="line">possible to change the value of <span class="number">1.</span> I suspect the behaviour of Python <span class="keyword">in</span> this case <span class="keyword">is</span> undefined.</span><br><span class="line"> </span><br><span class="line"><span class="comment"># 对于字符串：</span></span><br><span class="line">Incomputer science, string interning <span class="keyword">is</span> a method of storing only onecopy of each distinct string value, </span><br><span class="line">which must be immutable. Interning strings makes some stringprocessing tasks more time- <span class="keyword">or</span> space-efficient </span><br><span class="line">at the cost of requiring moretime when the string <span class="keyword">is</span> created <span class="keyword">or</span> interned. The distinct values are stored </span><br><span class="line">ina string intern pool.</span><br></pre></td></tr></table></figure>
<p>Python自动将 -5~256 的整数进行了缓存，当你将这些整数赋值给变量时，并不会重新创建对象，而是使用已经创建好的缓存对象。</p>
<p>python会将一定规则的字符串在字符串驻留池中，创建一份，当你将这些字符串赋值给变量时，并不会重新创建对象， 而是使用在字符串驻留池中创建好的对象。</p>
<p>其实，无论是缓存还是字符串驻留池，都是python做的一个优化，就是将~5-256的整数，和一定规则的字符串，放在一个‘池’（容器，或者字典）中，无论程序中那些变量指向这些范围内的整数或者字符串，那么他直接在这个‘池’中引用，言外之意，就是内存中之创建一个。</p>
<p>优点：能够提高一些字符串，整数处理人物在时间和空间上的性能；需要值相同的字符串，整数的时候，直接从‘池’里拿来用，避免频繁的创建和销毁，提升效率，节约内存。</p>
<p><strong>int：如上所述，小数据池的范围是-5~256 ，如果多个变量都是指向同一个（在这个范围内的）数字，他们在内存中指向的都是一个内存地址。</strong></p>
<p><strong>str:字符串从以下几个方面讨论：</strong></p>
<p>乘数为1时：</p>
<p>仅含大小写字母，数字，下划线，默认驻留。</p>
<p>含其他字符，长度&lt;=1,默认驻留。</p>
<p>含其他字符，长度&gt;1,默认驻留。</p>
<p>乘数&gt;=2时：</p>
<p>仅含大小写字母，数字，下划线，总长度&lt;=20,默认驻留。</p>
<h4 id="指定驻留"><a href="#指定驻留" class="headerlink" title="指定驻留"></a>指定驻留</h4><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">from</span> sys <span class="keyword">import</span> intern</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = intern(<span class="string">&quot;guo郭!#@*lp&quot;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = intern(<span class="string">&quot;guo郭!#@*lp&quot;</span>)</span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s1 <span class="keyword">is</span> s2)</span><br><span class="line"><span class="literal">True</span></span><br></pre></td></tr></table></figure>
<p>满足以上字符串的规则时，就符合小数据池的概念。</p>
<p>bool值就是True，False，无论你创建多少个变量指向True，False，那么他在内存中只存在一个。</p>
<p>看一下用了小数据池（驻留机制）的效率有多高：</p>
<p>显而易见，节省大量内存在字符串比较时，非驻留比较效率o(n)，驻留时比较效率o(1)。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p><strong>如果在同一代码块下，则采用同一代码块下的换缓存机制。</strong></p>
<p><strong>如果是不同代码块，则采用小数据池的驻留机制。</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>s1 = <span class="number">300</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>s2 = <span class="number">300</span>                  <span class="comment">#  直接python解释器运行，s1是一个代码块，s2是一个代码块</span></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>print(s1 <span class="keyword">is</span> s2)</span><br><span class="line"><span class="literal">False</span>                         <span class="comment">#  不同代码块走小数据池，300超出范围，所以为False</span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="comment">#在外面创建一个文件awa.py</span></span><br><span class="line">➜  ~ echo -e <span class="string">&quot;s1 = 300\ns2 = 300\nprint(s1 is s2)&quot;</span> &gt;&gt; awa.py</span><br><span class="line">➜  ~ python3<span class="number">.6</span> awa.py             <span class="comment"># 这个文件是一个代码块</span></span><br><span class="line"><span class="literal">True</span>                              <span class="comment">#  所以直接走代码块的缓存机制为True</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># 注： 如果在函数内生成两个函数，则这两个函数都生成300的情况下</span></span><br><span class="line"><span class="comment"># 因为一个函数体是一个代码块， python会为每个函数生成临时空间(开辟一块内存)</span></span><br><span class="line"><span class="comment"># 随函数执行完成而消散。所以结果会为False</span></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/03/26/python%E5%B0%8F%E6%95%B0%E6%8D%AE%E6%B1%A0/" data-id="cknublgbr000rv8uh34vr6ler" data-title="python小数据池" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-Vue视频直播" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/02/05/Vue%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" class="article-date">
  <time class="dt-published" datetime="2021-02-05T07:05:41.076Z" itemprop="datePublished">2021-02-05</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/02/05/Vue%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/">云主机实现视频直播</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="下载需要的推流工具OBS，拉流工具VLC"><a href="#下载需要的推流工具OBS，拉流工具VLC" class="headerlink" title="下载需要的推流工具OBS，拉流工具VLC"></a>下载需要的推流工具OBS，拉流工具VLC</h2><p><img src="https://img.jbzj.com/file_images/article/202006/202006050923345.jpg" alt="OBS推流工具"><br>OBS推流工具下载地址：<a target="_blank" rel="noopener" href="https://obsproject.com/">https://obsproject.com/</a></p>
<p><img src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3059244078,1961775731&fm=26&gp=0.jpg" alt="VLC拉流工具"><br>VLC拉流工具下载地址：<a target="_blank" rel="noopener" href="https://www.videolan.org/vlc/index.html">https://www.videolan.org/vlc/index.html</a></p>
<h2 id="云主机下载Nginx"><a href="#云主机下载Nginx" class="headerlink" title="云主机下载Nginx"></a>云主机下载Nginx</h2><p><strong>nginx</strong>：高性能多路复用服务器，让你支持同时响应多个客户连接，多线程、多进程开发模式。</p>
<p>在自己创建的目录下下载Nginx进行部署</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">wget http://nginx.org/download/nginx-1.10.3.tar.gz</span><br></pre></td></tr></table></figure>
<p>拉取Nginx进行下载</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone https://github.com/arut/nginx-rtmp-module</span><br></pre></td></tr></table></figure>
<p>解压下载好的nginx</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf nginx-1.10.3.tar.gz nginx-1.10.3/</span><br></pre></td></tr></table></figure>
<p>解压下载好的rtmp直播模块</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unzip nginx-rtmp-module-master.zip </span><br></pre></td></tr></table></figure>
<p>创建nginx的安装目录</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir /usr/local/nginx-rtmp</span><br></pre></td></tr></table></figure>
<p>在正式配置前安装依赖</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel </span><br></pre></td></tr></table></figure>
<p>配置nginx安装过程，搭配直播模块使用(nginx-rtmp-module)</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd nginx-1.10.3</span><br><span class="line">./configure --add-module=/home/rtmp-tools/nginx-rtmp-module/ --with-http_ssl_module --prefix=/usr/local/nginx-rtmp</span><br></pre></td></tr></table></figure>
<p>编译</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make</span><br></pre></td></tr></table></figure>
<p>安装</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">make install</span><br></pre></td></tr></table></figure>
<p>我们需要修改Nginx内部配置文件</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/local/nginx-rtmp/</span><br><span class="line">vim nginx.conf</span><br></pre></td></tr></table></figure>
<p>将nginx.conf内部修改为以下代码</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">rtmp &#123;</span><br><span class="line">    server &#123;</span><br><span class="line">        listen 1935;</span><br><span class="line">        application live &#123;</span><br><span class="line">            live on;</span><br><span class="line">            record off;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>根据配置启动服务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/usr/local/nginx-rtmp/sbin/nginx -c /usr/local/nginx-rtmp/conf/nginx.conf</span><br></pre></td></tr></table></figure>
<p>检查服务是否启动</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef | grep nginx     //如果显示两行以上，说明启动成功</span><br></pre></td></tr></table></figure>
<p>打开OBS设置推流地址和密钥<br><img src="https://img.jbzj.com/file_images/article/202006/202006050923346.jpg" alt="设置推流地址"></p>
<p>设置完后，回到OBS主页面。这时屏幕黑屏我们可以给来源添加一个显示器捕获，这时候就可以看到屏幕了，<br>当然我们还可以添加摄像头、录像等等。</p>
<p>然后点击推流按钮进行推流<br><img src="https://img.jbzj.com/file_images/article/202006/202006050923347.jpg" alt="OBS推流"></p>
<p>然后我们需要使其在vue中直播</p>
<h2 id="vue实现直播"><a href="#vue实现直播" class="headerlink" title="vue实现直播"></a>vue实现直播</h2><p>安装hls.js</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cnpm install hls.js -save</span><br></pre></td></tr></table></figure>
<p>安装dplayer</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cnpm install dplayer -save</span><br></pre></td></tr></table></figure>
<p>vue导入flv.js</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> flv <span class="keyword">from</span> <span class="string">&#x27;flv.js&#x27;</span></span><br></pre></td></tr></table></figure>
<p>配置flv</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">mounted</span>(<span class="params"></span>)</span> &#123;</span><br><span class="line">        <span class="comment">// 直播方面</span></span><br><span class="line">        <span class="keyword">var</span> videoElement = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;videoElement&#x27;</span>);</span><br><span class="line">        <span class="keyword">var</span> flvPlayer = flv.createPlayer(&#123;</span><br><span class="line">            type: <span class="string">&#x27;flv&#x27;</span>,</span><br><span class="line">            enableWorker: <span class="literal">true</span>,     <span class="comment">//浏览器端开启flv.js的worker,多进程运行flv.js</span></span><br><span class="line">            isLive: <span class="literal">true</span>,           <span class="comment">//直播模式</span></span><br><span class="line">            hasAudio: <span class="literal">false</span>,        <span class="comment">//关闭音频</span></span><br><span class="line">            hasVideo: <span class="literal">true</span>,</span><br><span class="line">            <span class="comment">// cors: true,</span></span><br><span class="line">            stashInitialSize: <span class="number">128</span>,</span><br><span class="line">            enableStashBuffer: <span class="literal">false</span>, <span class="comment">//播放flv时，设置是否启用播放缓存，只在直播起作用。</span></span><br><span class="line">            url: <span class="string">&#x27;http://8.131.52.82:8080/live?port=1935&amp;app=live&amp;stream=guo&#x27;</span></span><br><span class="line">        &#125;)</span><br><span class="line">        flvPlayer.attachMediaElement(videoElement);</span><br><span class="line">        flvPlayer.load();</span><br><span class="line">        flvPlayer.play();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>vue直播渲染</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--实现直播--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">video</span> <span class="attr">id</span>=<span class="string">&quot;videoElement&quot;</span> <span class="attr">controls</span> <span class="attr">muted</span>&gt;</span></span><br><span class="line">    Your browser is too old which doesn&#x27;t support HTML5 video.</span><br><span class="line"><span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/02/05/Vue%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" data-id="cknublgbg0008v8uhgb5uf60u" data-title="云主机实现视频直播" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" rel="tag">云主机实现视频直播</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-无限级分类自关联递归" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/02/04/%E6%97%A0%E9%99%90%E7%BA%A7%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/" class="article-date">
  <time class="dt-published" datetime="2021-02-04T02:43:51.225Z" itemprop="datePublished">2021-02-04</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/02/04/%E6%97%A0%E9%99%90%E7%BA%A7%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/">无限级分类自关联递归</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <hr>
<h3 id="无极限自关联查询"><a href="#无极限自关联查询" class="headerlink" title="无极限自关联查询"></a>无极限自关联查询</h3><p>views.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 无限自关联递归函数</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">function</span>(<span class="params"><span class="built_in">id</span></span>):</span></span><br><span class="line">    <span class="comment"># 接收id 查询结果集</span></span><br><span class="line">    query = User.objects.<span class="built_in">filter</span>(uid=<span class="built_in">id</span>)</span><br><span class="line">    <span class="comment"># 进行序列化</span></span><br><span class="line">    data = UserSer(query, many=<span class="literal">True</span>).data</span><br><span class="line">    <span class="comment"># 在序列化结果中添加下一级空列表列表，</span></span><br><span class="line">    data[<span class="string">&#x27;list&#x27;</span>] = []</span><br><span class="line">    <span class="comment"># 遍历结果集，递归调用并将调用结果存入上级列表</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> data:</span><br><span class="line">        data[<span class="string">&#x27;list&#x27;</span>].append(functions(i.uid))</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 返回结果集</span></span><br><span class="line">    <span class="keyword">return</span> data</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Path</span>(<span class="params">APIView</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self, request</span>):</span></span><br><span class="line">        <span class="comment"># 定义结果集列表</span></span><br><span class="line">        path_list = []</span><br><span class="line">        <span class="comment"># 查取所有信息</span></span><br><span class="line">        query = Path.objects.<span class="built_in">all</span>()</span><br><span class="line">        <span class="comment"># 遍历开始递归查询</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> data:</span><br><span class="line">            path_list.append(function(i.<span class="built_in">id</span>))</span><br><span class="line">        </span><br><span class="line">        retrun Response(path_list)</span><br></pre></td></tr></table></figure>
<p>models.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Path</span>(<span class="params">models.Model</span>):</span></span><br><span class="line">    name = models.CharField(max_leng=<span class="number">50</span>)</span><br><span class="line">    uid = models.ForeignKey(to=<span class="string">&#x27;self&#x27;</span>, on_delete=models.SETNULL, default=<span class="literal">None</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>serializer.py</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PathSer</span>(<span class="params">ModelSerializer</span>):</span></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Meta</span>:</span></span><br><span class="line">        model = Path</span><br><span class="line">        fields = <span class="string">&#x27;__all__&#x27;</span></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/02/04/%E6%97%A0%E9%99%90%E7%BA%A7%E5%88%86%E7%B1%BB%E8%87%AA%E5%85%B3%E8%81%94%E9%80%92%E5%BD%92/" data-id="cknublgc7001hv8uhcozra4gk" data-title="无限级分类自关联递归" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-webshell" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/02/03/webshell/" class="article-date">
  <time class="dt-published" datetime="2021-02-03T02:49:52.906Z" itemprop="datePublished">2021-02-03</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/02/03/webshell/">webshell</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="什么是webshell？"><a href="#什么是webshell？" class="headerlink" title="什么是webshell？"></a>什么是webshell？</h2><p>webshell就是以asp、php、jsp或者cgi等网页文件形式存在的一种代码执行环境，也可以将其称做为一种网页后门。<br>黑客在入侵了一个网站后，通常会将asp或php后门文件与网站服务器WEB目录下正常的网页文件混在一起，<br>然后就可以使用浏览器来访问asp或者php后门，得到一个命令执行环境，以达到控制网站服务器的目的。<br>顾名思义，“web”的含义是显然需要服务器开放web服务，“shell”的含义是取得对服务器某种程度上操作权限。<br>webshell常常被称为入侵者通过网站端口对网站服务器的某种程度上操作的权限。<br>由于webshell其大多是以动态脚本的形式出现，也有人称之为网站的后门工具。</p>
<p>web端使用Xterm.js或者其他的WebShell组件和WebSocket<br>后端只需要支持WebSocket和SSH协议的远程登录模块即可</p>
<h2 id="Web端shell的实现"><a href="#Web端shell的实现" class="headerlink" title="Web端shell的实现"></a>Web端shell的实现</h2><h3 id="客户端实现"><a href="#客户端实现" class="headerlink" title="客户端实现"></a>客户端实现</h3><p>安装xterm</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cnpm install xterm@3.1.0 --save</span><br></pre></td></tr></table></figure>
<p>在使用界面导包</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在线编程黑窗口导包</span></span><br><span class="line"><span class="keyword">import</span> &#123; Terminal &#125; <span class="keyword">from</span> <span class="string">&#x27;xterm&#x27;</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> attach <span class="keyword">from</span> <span class="string">&#x27;xterm/lib/addons/attach/attach&#x27;</span> <span class="comment">// 安装插件适，可以使用attach去添加</span></span><br><span class="line"><span class="keyword">import</span> * <span class="keyword">as</span> fit <span class="keyword">from</span> <span class="string">&#x27;xterm/lib/addons/fit/fit&#x27;</span> <span class="comment">// fit进行自适应大小的</span></span><br><span class="line">Terminal.applyAddon(attach) <span class="comment">// 添加插件</span></span><br><span class="line">Terminal.applyAddon(fit) <span class="comment">// 添加插件</span></span><br></pre></td></tr></table></figure>
<p>页面展示</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--实现在线编程黑窗口--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;terminal&quot;</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--黑窗口--&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>需要定义一个term空值来接收创建的黑窗口对象</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">    <span class="function"><span class="title">data</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span>&#123;           </span><br><span class="line">            term:<span class="literal">null</span>   <span class="comment">//在线编程黑窗口</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>进行数据操作</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在线编程黑窗口</span></span><br><span class="line">        <span class="comment">// 获取到了div标签</span></span><br><span class="line">        <span class="keyword">let</span> terminalContainer = <span class="built_in">document</span>.getElementById(<span class="string">&#x27;terminal&#x27;</span>)</span><br><span class="line">        <span class="comment">// 初始化黑窗口对象</span></span><br><span class="line">        <span class="built_in">this</span>.term = <span class="keyword">new</span> Terminal(<span class="built_in">this</span>.terminal)</span><br><span class="line">        <span class="comment">// 打开这个对象</span></span><br><span class="line">        <span class="built_in">this</span>.term.open(terminalContainer)</span><br><span class="line">        <span class="keyword">new</span> WebSocket(<span class="string">&#x27;ws://127.0.0.1:8000/webssh/&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket = <span class="keyword">new</span> WebSocket(<span class="string">&#x27;ws://127.0.0.1:8000/webssh/&#x27;</span>)</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket.onopen = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// 连接成功触发该方法</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;websocket is Connected...&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket.onclose = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// 连接关闭适触发的方法</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;websocket is Closed...&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.terminalSocket.onerror = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; <span class="comment">// 连接出错触发的方法</span></span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;damn Websocket is broken!&#x27;</span>)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">this</span>.term.attach(<span class="built_in">this</span>.terminalSocket)   <span class="comment">//绑定xterm到ws流中</span></span><br></pre></td></tr></table></figure>
<h3 id="服务端实现"><a href="#服务端实现" class="headerlink" title="服务端实现"></a>服务端实现</h3><p>使用paramiko与服务器建立交互，先进行安装，还需要安装dwebsocket</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">pip install paramiko</span><br><span class="line">pip install dwebsocket</span><br></pre></td></tr></table></figure>
<p>路由配置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.urls <span class="keyword">import</span> path</span><br><span class="line"><span class="keyword">from</span> .views <span class="keyword">import</span> *</span><br><span class="line"></span><br><span class="line">urlpatterns = [</span><br><span class="line">    path(<span class="string">&#x27;webssh/&#x27;</span>,webssh)</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<p><strong>django接收websocket</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> dwebsocket <span class="keyword">import</span> accept_websocket <span class="comment">#只允许连接web的视图</span></span><br><span class="line"><span class="comment">#完成一个响应websock的连接视图</span></span><br><span class="line"><span class="comment"># ws://127.0.0.1:8000/webssh/</span></span><br><span class="line"><span class="meta">@accept_websocket</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">webssh</span>(<span class="params">request</span>):</span></span><br><span class="line">    <span class="comment"># 1. 获取到连接对象</span></span><br><span class="line">    ws = request.websocket</span><br><span class="line">    <span class="keyword">while</span> <span class="number">1</span>:  <span class="comment"># 主线程: 只管发送</span></span><br><span class="line">      	<span class="comment"># 2. 阻塞ws接收发来的数据</span></span><br><span class="line">      	cmd = ws.wait()</span><br><span class="line">      	<span class="keyword">if</span> cmd:</span><br><span class="line">        		channel.send(cmd)  <span class="comment"># 发送到linux 去执行</span></span><br><span class="line">        <span class="keyword">else</span>:  <span class="comment"># 如果连接断开，那么cmd将会发一个空包</span></span><br><span class="line">          	<span class="keyword">break</span></span><br><span class="line">   	recv_thread.join()  <span class="comment"># 回收子线程</span></span><br><span class="line">   	ws.close()  <span class="comment"># 关闭ws连接</span></span><br></pre></td></tr></table></figure>
<p>将由django接收到的websocket数据发送到Linux中</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">make_ssh</span>(<span class="params">host, username, password, port=<span class="number">22</span></span>):</span></span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">    :host 主机地址</span></span><br><span class="line"><span class="string">    :username 用户名，一般是root</span></span><br><span class="line"><span class="string">    :password 密码</span></span><br><span class="line"><span class="string">    :port ssh协议的端口,22</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># 初始化一个ssh对象</span></span><br><span class="line">    sh = paramiko.SSHClient()</span><br><span class="line">    <span class="comment"># 设置对象连接密钥规则</span></span><br><span class="line">    sh.set_missing_host_key_policy(paramiko.AutoAddPolicy())</span><br><span class="line">    <span class="comment"># 连接</span></span><br><span class="line">    sh.connect(host, username=username, password=password)</span><br><span class="line">    <span class="comment"># 生成shell对象，把命令执行结果，模拟成一个终端的返回，带上花里胡哨的颜色</span></span><br><span class="line">    channle = sh.invoke_shell(term=<span class="string">&#x27;xterm&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> channle</span><br></pre></td></tr></table></figure>
<p>因为发送命令和接收不能在一条线上执行,所以现在要开辟一个子线程，专注于接收返回的命令。<br>主线程，专注的用来去发送命令，编写子线程方法，接收命令。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">recv_ssh_msg</span>(<span class="params">channle, ws</span>):</span></span><br><span class="line">    <span class="string">&#x27;&#x27;&#x27;</span></span><br><span class="line"><span class="string">        channle: 建立好的SSH连接通道</span></span><br><span class="line"><span class="string">        这个函数会不停的接收ssh通道返回的命令</span></span><br><span class="line"><span class="string">        返回到前端的ws套接字里</span></span><br><span class="line"><span class="string">    &#x27;&#x27;&#x27;</span></span><br><span class="line">    <span class="comment"># 判断shell连接对象是否没有退出</span></span><br><span class="line">    <span class="keyword">while</span> <span class="keyword">not</span> channle.exit_status_ready():</span><br><span class="line">      	<span class="comment"># 接收过程可能会因为没有任何返回而报错</span></span><br><span class="line">        <span class="keyword">try</span>:</span><br><span class="line">            buf = channle.recv(<span class="number">1024</span>) <span class="comment"># 接收命令的执行结果</span></span><br><span class="line">            ws.send(buf) <span class="comment"># 向Websocket通道返回</span></span><br><span class="line">        <span class="comment"># 接收不到会报错，但是报错没关系，继续重新尝试接受</span></span><br><span class="line">        <span class="keyword">except</span>:</span><br><span class="line">            <span class="keyword">break</span></span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/02/03/webshell/" data-id="cknublgc10015v8uh87698l6s" data-title="webshell" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/webshell/" rel="tag">webshell</a></li></ul>

    </footer>
  </div>
  
</article>



  
    <article id="post-redis键空间事件" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/blog1/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/" class="article-date">
  <time class="dt-published" datetime="2021-02-02T06:05:41.384Z" itemprop="datePublished">2021-02-02</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/blog1/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/">redis键空间事件</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <hr>
<h2 id="键空间的功能"><a href="#键空间的功能" class="headerlink" title="键空间的功能"></a>键空间的功能</h2><p>键空间通知使得客户端可以通过订阅频道或模式， 来接收那些以某种方式改动了 Redis 数据集的事件。<br>事件通过 Redis 的订阅与发布功能（pub/sub）来进行分发，<br>因此所有支持订阅与发布功能的客户端都可以在无须做任何修改的情况下， 直接使用键空间通知功能。</p>
<h2 id="键空间事件的类型"><a href="#键空间事件的类型" class="headerlink" title="键空间事件的类型"></a>键空间事件的类型</h2><p>对于每个修改数据库的操作，键空间通知都会发送两种不同类型的事件:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">publish __keyspace@1__:key expire</span><br><span class="line">publish __keyevent@1__:expire key</span><br></pre></td></tr></table></figure>
<p>订阅第一个频道<strong>keyspace@1</strong>:key可以接收1号数据库中所有修改键key的事件，<br>而订阅第二个频道<strong>keyevent@1</strong>:expire则可以接收1号数据库中所有执行expire命令的键。<br>以keyspace为前缀的频道被称为键空间通知，而以keyevent为前缀的频道则被称为键事件通知。</p>
<p>当我们执行最后一步expire key时：<br>键空间频道的订阅者将会接收到被执行的事件的名字。<br>键事件频道的订阅者将会接收到被执行事件的键的名字。</p>
<h2 id="键空间配置"><a href="#键空间配置" class="headerlink" title="键空间配置"></a>键空间配置</h2><p>键空间默认处于关闭状态，我们需要执行config set命令来开启或关闭键空间通知功能：<br>notify-keyspace-events选项的参数为空字符串时，功能关闭。<br>另一方面，当参数不是空字符串时，功能开启。<br>notify-keyspace-events的参数可以是以下字符的任意组合，它指定了服务器该发送哪些类型的通知：</p>
<hr>
<table>
<thead>
<tr>
<th align="left">字符</th>
<th align="left">发送的通知</th>
</tr>
</thead>
<tbody><tr>
<td align="left">K(必要)</td>
<td align="left">键空间通知，所有通知以 <strong>keyspace@<db></strong> 为前缀</td>
</tr>
<tr>
<td align="left">E(必要)</td>
<td align="left">键事件通知，所有通知以 <strong>keyevent@<db></strong> 为前缀</td>
</tr>
<tr>
<td align="left">g</td>
<td align="left">DEL 、EXPIRE 、RENAME等类型无关的通用命令的通知</td>
</tr>
<tr>
<td align="left">$</td>
<td align="left">字符串命令的通知</td>
</tr>
<tr>
<td align="left">l</td>
<td align="left">列表命令的通知</td>
</tr>
<tr>
<td align="left">s</td>
<td align="left">集合命令的通知</td>
</tr>
<tr>
<td align="left">h</td>
<td align="left">哈希命令的通知</td>
</tr>
<tr>
<td align="left">z</td>
<td align="left">有序集合命令的通知</td>
</tr>
<tr>
<td align="left">x</td>
<td align="left">过期事件：每当有过期键被删除时发送</td>
</tr>
<tr>
<td align="left">e</td>
<td align="left">驱逐(evict)事件：每当有键因为maxmemory政策而被删除时发送</td>
</tr>
<tr>
<td align="left">A</td>
<td align="left">参数g$lshzxe的别名</td>
</tr>
</tbody></table>
<p>以上字符K和E是必要条件，每次的参数必须有其一，否则通知不会被分发。</p>
<h2 id="命令产生通知"><a href="#命令产生通知" class="headerlink" title="命令产生通知"></a>命令产生通知</h2><p><strong>只有当键发生变化时，通知才会产生</strong><br>以下列表记录了不同命令所产生的不同通知：</p>
<p>DEL key [key …] 命令为每个被删除的键产生一个 del 通知。</p>
<p>RENAME key newkey 产生两个通知：为来源键（source key）产生一个 rename_from 通知，并为目标键（destination key）产生一个 rename_to 通知。</p>
<p>EXPIRE key seconds 和 EXPIREAT key timestamp 在键被正确设置过期时间时产生一个 expire 通知。当 EXPIREAT key timestamp 设置的时间已经过期，或者 EXPIRE key seconds 传入的时间为负数值时，键被删除，并产生一个 del 通知。</p>
<p>SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern …]] [ASC | DESC] [ALPHA] [STORE destination] 在命令带有 STORE 参数时产生一个 sortstore 事件。如果 STORE 指示的用于保存排序结果的键已经存在，那么程序还会发送一个 del 事件。</p>
<p>SET key value [EX seconds] [PX milliseconds] [NX|XX] 以及它的所有变种（SETEX key seconds value 、 SETNX key value 和 GETSET key value）都产生 set 通知。其中 SETEX key seconds value 还会产生 expire 通知。</p>
<p>MSET key value [key value …] 为每个键产生一个 set 通知。</p>
<p>SETRANGE key offset value 产生一个 setrange 通知。</p>
<p>INCR key 、 DECR key 、 INCRBY key increment 和 DECRBY key decrement 都产生 incrby 通知。</p>
<p>INCRBYFLOAT key increment 产生 incrbyfloat 通知。</p>
<p>APPEND key value 产生 append 通知。</p>
<p>LPUSH key value [value …] 和 LPUSHX key value 都产生单个 lpush 通知，即使有多个输入元素时，也是如此。</p>
<p>RPUSH key value [value …] 和 RPUSHX key value 都产生单个 rpush 通知，即使有多个输入元素时，也是如此。</p>
<p>RPOP key 产生 rpop 通知。如果被弹出的元素是列表的最后一个元素，那么还会产生一个 del 通知。</p>
<p>LPOP key 产生 lpop 通知。如果被弹出的元素是列表的最后一个元素，那么还会产生一个 del 通知。</p>
<p>LINSERT key BEFORE|AFTER pivot value 产生一个 linsert 通知。</p>
<p>LSET key index value 产生一个 lset 通知。</p>
<p>LTRIM key start stop 产生一个 ltrim 通知。如果 LTRIM key start stop 执行之后，列表键被清空，那么还会产生一个 del 通知。</p>
<p>RPOPLPUSH source destination 和 BRPOPLPUSH source destination timeout 产生一个 rpop 通知，以及一个 lpush 通知。两个命令都会保证 rpop 的通知在 lpush 的通知之前分发。如果从键弹出元素之后，被弹出的列表键被清空，那么还会产生一个 del 通知。</p>
<p>HSET hash field value 、 HSETNX hash field value 和 HMSET 都只产生一个 hset 通知。</p>
<p>HINCRBY 产生一个 hincrby 通知。</p>
<p>HINCRBYFLOAT 产生一个 hincrbyfloat 通知。</p>
<p>HDEL 产生一个 hdel 通知。如果执行 HDEL 之后，哈希键被清空，那么还会产生一个 del 通知。</p>
<p>SADD key member [member …] 产生一个 sadd 通知，即使有多个输入元素时，也是如此。</p>
<p>SREM key member [member …] 产生一个 srem 通知，如果执行 SREM key member [member …] 之后，集合键被清空，那么还会产生一个 del 通知。</p>
<p>SMOVE source destination member 为来源键（source key）产生一个 srem 通知，并为目标键（destination key）产生一个 sadd 事件。</p>
<p>SPOP key 产生一个 spop 事件。如果执行 SPOP key 之后，集合键被清空，那么还会产生一个 del 通知。</p>
<p>SINTERSTORE destination key [key …] 、 SUNIONSTORE destination key [key …] 和 SDIFFSTORE destination key [key …] 分别产生 sinterstore 、 sunionostore 和 sdiffstore 三种通知。如果用于保存结果的键已经存在，那么还会产生一个 del 通知。</p>
<p>ZINCRBY key increment member 产生一个 zincr 通知。（译注：非对称，请注意。）</p>
<p>ZADD key score member [[score member] [score member] …] 产生一个 zadd 通知，即使有多个输入元素时，也是如此。</p>
<p>ZREM key member [member …] 产生一个 zrem 通知，即使有多个输入元素时，也是如此。如果执行 ZREM key member [member …] 之后，有序集合键被清空，那么还会产生一个 del 通知。</p>
<p>ZREMRANGEBYSCORE key min max 产生一个 zrembyscore 通知。（译注：非对称，请注意。）如果用于保存结果的键已经存在，那么还会产生一个 del 通知。</p>
<p>ZREMRANGEBYRANK key start stop 产生一个 zrembyrank 通知。（译注：非对称，请注意。）如果用于保存结果的键已经存在，那么还会产生一个 del 通知。</p>
<p>ZINTERSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX] 和 ZUNIONSTORE destination numkeys key [key …] [WEIGHTS weight [weight …]] [AGGREGATE SUM|MIN|MAX] 分别产生 zinterstore 和 zunionstore 两种通知。如果用于保存结果的键已经存在，那么还会产生一个 del 通知。</p>
<p>每当一个键因为过期而被删除时，产生一个 expired 通知。</p>
<p>每当一个键因为 maxmemory 政策而被删除以回收内存时，产生一个 evicted 通知。</p>
<p><strong>对通知进行验证</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> redis-cli config <span class="built_in">set</span> notify-keyspace-events <span class="string">&#x27;AKE&#x27;</span></span></span><br><span class="line"><span class="meta">$</span><span class="bash"> redis-cli --csv psubscribe <span class="string">&#x27;__key*__:*&#x27;</span></span></span><br><span class="line">Reading messages... (press Ctrl-C to quit)</span><br><span class="line">&quot;psubscribe&quot;,&quot;__key*__:*&quot;,1</span><br></pre></td></tr></table></figure>
<p><strong>在其他终端里用 Redis 客户端发送命令，得到通知信息</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&quot;pmessage&quot;,&quot;__key*__:*&quot;,&quot;__keyspace@1__:key&quot;,&quot;del&quot;</span><br><span class="line">&quot;pmessage&quot;,&quot;__key*__:*&quot;,&quot;__keyevent@1__:del&quot;,&quot;key&quot;</span><br></pre></td></tr></table></figure>
      
    </div>
    <footer class="article-footer">
      <a data-url="http://shangguan-lingfeng.gitee.io/blog1/2021/02/02/redis%E9%94%AE%E7%A9%BA%E9%97%B4%E4%BA%8B%E4%BB%B6/" data-id="cknublgbw000wv8uhfifng1c3" data-title="redis键空间事件" class="article-share-link">Teilen</a>
      
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li></ul>

    </footer>
  </div>
  
</article>



  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/blog1/page/2/">&laquo; zurück</a><a class="page-number" href="/blog1/">1</a><a class="page-number" href="/blog1/page/2/">2</a><span class="page-number current">3</span><a class="page-number" href="/blog1/page/4/">4</a><a class="extend next" rel="next" href="/blog1/page/4/">weiter &raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/CentOS/" rel="tag">CentOS</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/Flask/" rel="tag">Flask</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/django/" rel="tag">django</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/docker/" rel="tag">docker</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/gitee/" rel="tag">gitee</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/linux/" rel="tag">linux</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/mysql/" rel="tag">mysql</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python/" rel="tag">python</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" rel="tag">python基础知识</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/redis/" rel="tag">redis</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/vue/" rel="tag">vue</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/webshell/" rel="tag">webshell</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" rel="tag">云主机实现视频直播</a></li><li class="tag-list-item"><a class="tag-list-link" href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" rel="tag">关于第三方</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/blog1/tags/CentOS/" style="font-size: 17.5px;">CentOS</a> <a href="/blog1/tags/Flask/" style="font-size: 10px;">Flask</a> <a href="/blog1/tags/django/" style="font-size: 12.5px;">django</a> <a href="/blog1/tags/docker/" style="font-size: 12.5px;">docker</a> <a href="/blog1/tags/gitee/" style="font-size: 10px;">gitee</a> <a href="/blog1/tags/linux/" style="font-size: 10px;">linux</a> <a href="/blog1/tags/mysql/" style="font-size: 12.5px;">mysql</a> <a href="/blog1/tags/python/" style="font-size: 15px;">python</a> <a href="/blog1/tags/python%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/" style="font-size: 17.5px;">python基础知识</a> <a href="/blog1/tags/redis/" style="font-size: 12.5px;">redis</a> <a href="/blog1/tags/vue/" style="font-size: 20px;">vue</a> <a href="/blog1/tags/webshell/" style="font-size: 10px;">webshell</a> <a href="/blog1/tags/%E4%BA%91%E4%B8%BB%E6%9C%BA%E5%AE%9E%E7%8E%B0%E8%A7%86%E9%A2%91%E7%9B%B4%E6%92%AD/" style="font-size: 10px;">云主机实现视频直播</a> <a href="/blog1/tags/%E5%85%B3%E4%BA%8E%E7%AC%AC%E4%B8%89%E6%96%B9/" style="font-size: 12.5px;">关于第三方</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archiv</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/05/">五月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/02/">二月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/blog1/archives/2021/01/">一月 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">letzter Beitrag</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/blog1/2021/05/14/docker+CentOS+mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">docker+CentOS+mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/13/mysql%E5%AE%9E%E7%8E%B0%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB(%E5%9C%A8%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5%E5%9F%BA%E7%A1%80%E4%B8%8A)/">mysql实现读写分离</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/%E5%9C%A8%E9%A1%B9%E7%9B%AE%E4%B8%AD%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3/">在项目中自动生成接口文档</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/12/mysql%E5%AE%9E%E7%8E%B0%E4%B8%BB%E4%BB%8E%E5%90%8C%E6%AD%A5/">mysql实现主从同步</a>
          </li>
        
          <li>
            <a href="/blog1/2021/05/09/CentOS%E9%83%A8%E7%BD%B2flask%E9%A1%B9%E7%9B%AE/">nginx部署flask项目</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2021 Jerry<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/blog1/" class="mobile-nav-link">Home</a>
  
    <a href="/blog1/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/blog1/js/jquery-3.4.1.min.js"></script>



  
<script src="/blog1/fancybox/jquery.fancybox.min.js"></script>




<script src="/blog1/js/script.js"></script>





  </div>
</body>
</html>